import { _get, _post, loadimg, _post1 } from "../../../utils/request";
import util, { formatDate } from "../../../utils/util";
import { debounce, throttle } from "../../../utils/debounce";

const app = getApp();
Page({
    data: {
        fubiao_list: [],
    },
    bindKeyInput(e) {
        console.log("input", e);
        this.data.fubiao_list[e.currentTarget.dataset.index1].content[
            e.currentTarget.dataset.index2
        ].params[e.currentTarget.dataset.index3][
            e.currentTarget.dataset.index4
        ].data = e.detail.value;
        this.setData({
            fubiao_list: this.data.fubiao_list,
        });
    },
    onSubmit: throttle(function(e) {
        console.log("提交附表", this.data.fubiao_list);
        let params = [];
        for (let i = 0; i < this.data.fubiao_list.length; i++) {
            params.push({
                content: JSON.stringify(this.data.fubiao_list[i].content),
                id: this.data.fubiao_list[i].id,
                itemId: app.testing_id,
                userId: app.userId ? app.userId : 21,
                templateId: app.templateId
            });
        }

        _post1(`/myTest/submitAttachResult`, JSON.stringify(params)).then((res) => {
            console.log("提交结果", res);
            dd.reLaunch({
                url: "/pages/mytest/testing/testing",
            });
        });
    }, 2000),
    add(e) {
        let index1 = e.target.dataset.index1;
        let index2 = e.target.dataset.index2;
        console.log("index1", index1);
        console.log("index2", index2);
        let list = JSON.parse(
            JSON.stringify(this.data.fubiao_list[index1].content[index2].params[0])
        );
        console.log("list", list);
        for (let i = 0; i < list.length; i++) {
            list[i].data = "";
        }
        console.log(this.data.fubiao_list[index1].content[index2].params[0]);
        this.data.fubiao_list[index1].content[index2].params.push(list);
        this.setData({
            fubiao_list: this.data.fubiao_list,
        });
        // this.setData({
        //     ["fubiao_list[" + addd + "]"]: {
        //         title: this.data.fubiao_list[addd].title,
        //         fubiao_list1: this.data.fubiao_list[addd].fubiao_list1.concat([{
        //             name: "",
        //             num: "",
        //             data: "",
        //         }]),
        //     },
        // });
    },
    getList() {
        _get(`/myTest/selectAttachBy`, {
            itemId: app.testing_id,
            templateIds: app.templateId,
        }).then((res) => {
            console.log("附表-未处理", res.data);
            let table = res.data.data.map((item) => {
                item.content = JSON.parse(item.content);
                return item;
            });
            // table.forEach((item1, index1) => {
            //     item1.content.forEach((item2, index2) => {
            //         let arr = [];
            //         arr.push(item2.params);
            //         item2.params = arr;
            //         for (let i = 0; i < item2.params[0].length; i++) {
            //             item2.params[0][i].data = "";
            //         }
            //     });
            // });
            this.setData({
                fubiao_list: res.data.data,
            });
            console.log("附表", res.data.data);
        });
    },
    onLoad() {
        dd.setNavigationBar({
            title: "附表",
            backgroundColor: "#142e4f",
            success() {},
            fail() {},
        });
        this.getList();
    },
});